package servlet;

import dao.ItemDAOImpl;
import pojo.Item;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/item")
public class ItemServlet extends BaseServlet {

    // 添加物品
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int itemNumber = Integer.parseInt(req.getParameter("itemNumber"));
        int warehouseNumber = Integer.parseInt(req.getParameter("warehouseNumber"));
        String itemName = req.getParameter("itemName");
        String itemPosition = req.getParameter("itemPosition");
        String itemSize = req.getParameter("itemSize");
        String itemType = req.getParameter("itemType");
        int itemQuantity = Integer.parseInt(req.getParameter("itemQuantity"));
        double itemPrice = Double.parseDouble(req.getParameter("itemPrice"));
        // Assuming delivery time is in milliseconds since epoch
        java.sql.Timestamp itemDeliveryTime = new java.sql.Timestamp(System.currentTimeMillis());

        Item item = new Item(itemNumber, warehouseNumber, itemName, itemPosition, itemSize, itemType, itemQuantity, itemPrice, itemDeliveryTime);
        ItemDAOImpl.add(item);
        sendJsonResponse(resp, new ApiResult(200, "Item added successfully.", null));
    }

    // 删除物品
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int itemNumber = Integer.parseInt(req.getParameter("itemNumber"));
        ItemDAOImpl.delete(itemNumber);
        sendJsonResponse(resp, new ApiResult(200, "Item deleted successfully.", null));
    }

    // 更新物品
    protected void updateItem(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int itemNumber = Integer.parseInt(req.getParameter("itemNumber"));
        int warehouseNumber = Integer.parseInt(req.getParameter("warehouseNumber"));
        String itemName = req.getParameter("itemName");
        String itemPosition = req.getParameter("itemPosition");
        String itemSize = req.getParameter("itemSize");
        String itemType = req.getParameter("itemType");
        int itemQuantity = Integer.parseInt(req.getParameter("itemQuantity"));
        double itemPrice = Double.parseDouble(req.getParameter("itemPrice"));
        java.sql.Timestamp itemDeliveryTime = new java.sql.Timestamp(System.currentTimeMillis());
        Item item = new Item(itemNumber, warehouseNumber, itemName, itemPosition, itemSize, itemType, itemQuantity, itemPrice, itemDeliveryTime);
        ItemDAOImpl.update(item);
        sendJsonResponse(resp, new ApiResult(200, "Item updated successfully.", null));
    }

    // 获取所有物品
    protected void getAllItems(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Item> items = ItemDAOImpl.getAll();
        sendJsonResponse(resp, new ApiResult(200, "Success", items));
    }

    // 根据编号获取物品
    protected void getItemById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int itemNumber = Integer.parseInt(req.getParameter("itemNumber"));
        Item item = ItemDAOImpl.getById(itemNumber);
        sendJsonResponse(resp, new ApiResult(200, "Success", item));
    }

    // 获取物品数量
    protected void getItemCount(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int count = ItemDAOImpl.getCount();
        sendJsonResponse(resp, new ApiResult(200, "Success", count));
    }

    // 分页获取物品
    protected void getItemsByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Item> items = ItemDAOImpl.getByPage(offset, pageSize);
        sendJsonResponse(resp, new ApiResult(200, "Success", items));
    }

    // 根据关键字搜索物品
    protected void findItemsByKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        List<Item> items = ItemDAOImpl.findByKeyword(keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", items));
    }

    // 根据关键字分页搜索物品
    protected void findItemsByPageAndKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Item> items = ItemDAOImpl.findByPageAndKeyword(offset, pageSize, keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", items));
    }
}
